# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
from typing import Generator
import numpy as np
import pandas as pd 
import os
import pymongo
import copy

# label should be a list.
# 这儿的数据也不应该被 splited
def readFile(path:str, col:pymongo.collection.Collection):
    # our sample step.
    period = 0.02
    period_points = 200
    sampled_freq = period_points / period
    # this data can got from the header.
    # original sample rate
    with open(path, mode="r") as f:
        while True:
            line = f.readline()
            if "SampleRate" in line:
                break
    line = line.split(",")
    # Sample rate of the data.
    source_freq = float(line[1].strip())
    step = source_freq / sampled_freq
    # convert to int for step only using integer
    step = int(step)
    # get the labels
    path_splited = path.split("/")
    # start with . means is the hidden data
    if path_splited[-1][0] == ".":
        # print(path)
        return
    labels = []
    # "0" as the start name. 
    if path_splited[-1][0] == "0":
        labels.append(["0"])
    else:
        labels.append([path_splited[-1][0]])
        # isdigit is a function for str.m
        if path_splited[-1][1].isdigit():
            labels.append([path_splited[-1][1]])
    # Now the labels is a list of list.
    # read source data.
    # source data shape is (timesteps, channels)
    sour_data = np.loadtxt(path, skiprows=16, delimiter=",", usecols=range(1,4))
    sour_data = sour_data.T
    resampled_data = sour_data[:,::step]
    resample_len = resampled_data.shape[1]
    delimiter = int(resample_len / 2)
    normal_label = ["0"] * delimiter
    for idx, label_list in enumerate(labels):
        label_list = label_list*(resample_len - delimiter)
        temp_list = copy.deepcopy(normal_label)
        temp_list.extend(label_list)
        labels[idx] = temp_list

    names = ["ia", "ib", "ic"]
    myDict = dict({})
    for name, i in zip(names, range(3)):
        myDict[name] = resampled_data[i,:].tolist()
    myDict["label"] = labels
    # save data.
    col.insert_one(myDict)
    

def retrieve_files(path:str) -> Generator:
    path_gen = os.walk(path)
    for root, _, files in path_gen:
        for name in files:
           yield(os.path.join(root,name))
        

if __name__ == "__main__":
    scriptDir = os.path.dirname(os.path.realpath(__file__))
    basepath = os.path.dirname(os.path.dirname(scriptDir))
    dataDir = os.path.join(basepath, "data")
    dataDir = os.path.join(dataDir, "邓茜实验数据")
    client = pymongo.MongoClient("mongodb://127.0.0.1:27017/")
    db  = client["Power_Fault"]
    col_sour = db["data_target_exper"]
    print(type(col_sour))
    file_gen = retrieve_files(dataDir)
    for file in file_gen:
        try:
            readFile(file, col_sour)
            # break
        except:
            print(file)
            raise
    